package com.easyexcle.demo.pdf.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.URLUtil;
import com.easyexcle.demo.pdf.Base64Utils;
import com.easyexcle.demo.pdf.PdfUtil;
import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @Author: lkz
 * @Title: PdfRestApi
 * @Description: TODO
 * @Date: 2022/12/27 16:55
 * 参考 https://blog.csdn.net/qiaodaima0/article/details/126271272
 */
@Controller
@RequestMapping("/velocity")
public class PdfRestApi {

    @RequestMapping("/getPdf")
    public void get(HttpServletResponse response){
        response.reset();
        response.setContentType("application/pdf");
        String filename = System.currentTimeMillis()+".pdf";
        response.addHeader("Content-Disposition", "inline; filename=" + URLUtil.encode(filename, CharsetUtil.CHARSET_UTF_8));
        VelocityContext context = new VelocityContext();

        context.put("name", "彭也行");
        context.put("gender", 1);
        context.put("birthDate", DateUtil.formatDateTime(new Date()));
        context.put("phone", "13666666666");

        List<Map<String, Object>> eduList = new ArrayList<>();
        // 小学
        Map<String, Object> primarySchoolMap = new HashMap<>();
        primarySchoolMap.put("name", "小学");
        List<Map<String, Object>> scoreList = new ArrayList<>();
        // 语文成绩
        Map<String, Object> chineseScore = new HashMap<>();
        chineseScore.put("subject", "语文");
        chineseScore.put("num", 60);
        // 数学成绩
        Map<String, Object> mathScore = new HashMap<>();
        mathScore.put("subject", "数学");
        mathScore.put("num", 99);
        scoreList.add(chineseScore);
        scoreList.add(mathScore);
        primarySchoolMap.put("itemList", scoreList);
        primarySchoolMap.put("size", scoreList.size());

        // 初中
        Map<String, Object> middleSchoolMap = new HashMap<>();
        middleSchoolMap.put("name", "初中");
        List<Map<String, Object>> middleScoreList = new ArrayList<>();
        // 语文成绩
        Map<String, Object> middleChineseScore = new HashMap<>();
        middleChineseScore.put("subject", "语文");
        middleChineseScore.put("num", 60);
        // 数学成绩
        Map<String, Object> middleMathScore = new HashMap<>();
        middleMathScore.put("subject", "数学");
        middleMathScore.put("num", 99);
        // 英语
        Map<String, Object> middleEnScore = new HashMap<>();
        middleEnScore.put("subject", "英语");
        middleEnScore.put("num", 55);

        middleScoreList.add(middleChineseScore);
        middleScoreList.add(middleMathScore);
        middleScoreList.add(middleEnScore);
        middleScoreList.add(middleEnScore);
        middleScoreList.add(middleEnScore);
        middleScoreList.add(middleEnScore);
        middleScoreList.add(middleEnScore);
        middleScoreList.add(middleEnScore);
        middleSchoolMap.put("itemList", middleScoreList);
        middleSchoolMap.put("size", middleScoreList.size());

        eduList.add(primarySchoolMap);
        eduList.add(middleSchoolMap);
        context.put("eduList", eduList);
        try(ServletOutputStream outputStream = response.getOutputStream()){
            PdfUtil.pdfFile(context, "demo.html", outputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @GetMapping("/base")
    public String testBase64(){
        String str="https://lmg.jj20.com/up/allimg/1114/040221103339/210402103339-8-1200.jpg";
       return Base64Utils.getBase64FromHttp(str);
    }


}
